package com.xl.rentkeeper.act.kanfang;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.RoomStatus;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.BaseAct;
import com.xl.rentkeeper.act.SearchRoomInfoAct;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.util.TipMapping;
import com.xl.rentkeeper.util.Util;
import com.xl.rentkeeper.view.TimePopupWindow;
import com.xl.rentkeeper.view.TitleBar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2015/8/17.
 */
public class AddLookRoomInfoAct extends BaseAct implements View.OnClickListener {
    private TimePopupWindow pwTime;
    private TextView mTVLookRoomTime;
    private long mTime = 0;
    private Dialog mDialog;

    private EditText mETUserName;
    private EditText mETUserJob;
    private EditText mETUserPhone;
    private EditText mETCheckInDay;
    private TextView mTVRoomName1;
    private TextView mTVRoomName2;
    private TextView mTVRoominfoStatus1;
    private TextView mTVRoominfoStatus2;
    private TextView mTVRoominfoPrice1;
    private TextView mTVRoominfoPrice2;
    private EditText mETRemark;

    private Button mBTMen;
    private Button mBTWomen;

    private String mSex = "男";
    RoomInfo[] roomInfos = new RoomInfo[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_look_room);

        TitleBar tb = (TitleBar) findViewById(R.id.tb);
        setTranslucentStatus();
        initTitleBar(tb);
        initView();
        setTime();
    }

    private void initView() {
        mTVLookRoomTime = (TextView) findViewById(R.id.tv_lookRoomTime);

        mETUserName = (EditText) findViewById(R.id.et_name);
        mETUserJob = (EditText) findViewById(R.id.et_job);
        mETUserPhone = (EditText) findViewById(R.id.et_phone);
        mETCheckInDay = (EditText) findViewById(R.id.et_checkInDays);
        mTVRoomName1 = (TextView) findViewById(R.id.et_roomName1);
        mTVRoomName2 = (TextView) findViewById(R.id.et_roomName2);
        mTVRoominfoStatus1 = (TextView) findViewById(R.id.tv_roominfo_status1);
        mTVRoominfoStatus2 = (TextView) findViewById(R.id.tv_roominfo_status2);
        mTVRoominfoPrice1 = (TextView) findViewById(R.id.tv_price1);
        mTVRoominfoPrice2 = (TextView) findViewById(R.id.tv_price2);
        mETRemark = (EditText) findViewById(R.id.et_remark);
        mBTMen = (Button) findViewById(R.id.bt_man);
        mBTWomen = (Button) findViewById(R.id.bt_women);

        mBTMen.setSelected(true);

        mBTMen.setOnClickListener(this);
        mBTWomen.setOnClickListener(this);
        mTVRoomName1.setOnClickListener(this);
        mTVRoomName2.setOnClickListener(this);
    }

    public void submit(View v) {
        ArrayList<RoomInfo> rooms = new ArrayList<>();
        String userName = mETUserName.getText().toString().trim();
        if ("".equals(userName)) {
            App.showToast(R.string.addLookRoom_fillIn_name);
            return;
        }

        String userJob = mETUserJob.getText().toString().trim();
        if ("".equals(userJob)) {
            App.showToast(R.string.addLookRoom_fillIn_job);
            return;
        }
        // addLookRoom_fillIn_name
        String userPhone = mETUserPhone.getText().toString().trim();
        if ("".equals(userPhone)) {
            App.showToast(R.string.addLookRoom_fillIn_phone);
            return;
        }

        String checkInDay = mETCheckInDay.getText().toString().trim();
        if ("".equals(checkInDay)) {
            App.showToast(R.string.addLookRoom_fillIn_checkInDay);
            return;
        }

        if (mTime == 0) {
            App.showToast(R.string.addLookRoom_fillIn_lookTime);
            return;
        }

        long nowTime = new Date().getTime();
        nowTime = nowTime - nowTime%10000;
        if (mTime < nowTime){
            App.showToast("看房时间不能在当前时间之前");
            return;
        }

        if (roomInfos[0] == null && roomInfos[1] == null) {
            App.showToast("请至少选择一个房间");
            return;
        } else {
            RoomInfo roomInfo;
            if (roomInfos[0] != null) {
                rooms.add(roomInfos[0]);
            }
            if (roomInfos[1] != null) {
                rooms.add(roomInfos[1]);
            }
        }

        mDialog = DialogUtil.showProgressDialog(this, R.string.loading);

        String remark = mETRemark.getText().toString().trim();
        OrderLogic.getInstance().addVieRoom(userName, mSex, userPhone, userJob, mTime, remark, checkInDay, rooms);

    }


    private void setTime() {
        //tvTime=(TextView) findViewById(R.id.tvTime);
        //时间选择器
        pwTime = new TimePopupWindow(this, TimePopupWindow.Type.ALL);
        //时间选择后回调
        pwTime.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                mTime = date.getTime();
                mTVLookRoomTime.setText(getTime(date));
            }
        });
        //弹出时间选择器
        mTVLookRoomTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeKeyboard(mETUserName);
                closeKeyboard(mETUserJob);
                closeKeyboard(mETCheckInDay);
                closeKeyboard(mETUserPhone);
                pwTime.showAtLocation(mTVLookRoomTime, Gravity.BOTTOM, 0, 0, new Date());
            }
        });

    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public String[] monitorEvents() {
        return new String[]{PMS_LOGIC_AddView};
    }

    @Override
    public void onEvent(String event, boolean ret, String errInfo, Object[] data) {
        super.onEvent(event, ret, errInfo, data);
        if (event.equals(OrderLogic.PMS_LOGIC_AddView)) {
            if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
            if (ret) {
                finish();
            } else {
                App.showToast(errInfo);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.et_roomName1:
                intent = new Intent(this, SearchRoomInfoAct.class);
                intent.putExtra("position", 0);
                startActivityForResult(intent, 1);
                break;
            case R.id.et_roomName2:
                intent = new Intent(this, SearchRoomInfoAct.class);
                intent.putExtra("position", 1);
                startActivityForResult(intent, 1);
                break;
            case R.id.bt_man:
                mBTMen.setSelected(true);
                mBTMen.setClickable(true);
                mBTWomen.setSelected(false);
                mSex = "男";
                break;
            case R.id.bt_women:
                mBTWomen.setSelected(true);
                mBTWomen.setClickable(true);
                mBTMen.setSelected(false);
                mSex = "女";
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            RoomInfo room = (RoomInfo) data.getSerializableExtra("info");
            int position = data.getIntExtra("position", -1);
            if (room == null || position == -1) return;
            if (position == 0) {
                if (roomInfos[1] != null) {
                    if (room.roomId.equals(roomInfos[1].roomId)) {
                        App.showToast(R.string.select_room_repeat);
                        return;
                    }
                }
            } else {
                if (roomInfos[0] != null) {
                    if (room.roomId.equals(roomInfos[0].roomId)) {
                        App.showToast(R.string.select_room_repeat);
                        return;
                    }
                }
            }
            roomInfos[position] = room;
            refreshView(room, position);
        }
    }

    private void refreshView(RoomInfo room, int position) {
        if (position == 0) {
            mTVRoomName1.setText(roomInfos[position].name);
            Util.setRoomStatus(mTVRoominfoStatus1, roomInfos[position], "add_look_room");
            Util.setRoomPrice(mTVRoominfoPrice1, roomInfos[position], "add_look_room");
        } else {
            mTVRoomName2.setText(roomInfos[position].name);
            Util.setRoomStatus(mTVRoominfoStatus2, roomInfos[position], "add_look_room");
            Util.setRoomPrice(mTVRoominfoPrice2, roomInfos[position], "add_look_room");
        }
    }
}
